﻿using System;
using System.Collections.Generic;
using System.Web;
using DotNetNuke.Entities.Tabs;

namespace HeroldIT.Dnn.Modules.Compatibility
{
    /// <summary>
    /// Provides a compatibility layer for the
    /// <see cref="DotNetNuke.Entities.Tabs.TabController"/> class.
    /// </summary>
    public abstract class TabController
    {
        #region constants

        private static readonly string TabControllerItemKey = typeof(TabController).FullName + "_Instance";

        #endregion

        #region static properties

        private static readonly object instanceLock = new object();

        /// <summary>
        /// Gets an instance of the PortalSettings compatibility class.
        /// </summary>
        public static TabController Instance
        {
            get
            {
                if (null == HttpContext.Current.Items[TabControllerItemKey])
                {
                    lock (instanceLock)
                    {
                        if (null == HttpContext.Current.Items[TabControllerItemKey])
                        {
                            HttpContext.Current.Items[TabControllerItemKey] =
                                Environment.DotNetNukeVersion.Major < 5
                                    ? new Dnn4.TabController()
                                    : AppDomain.CurrentDomain.CreateInstanceAndUnwrap(
                                          "HeroldIT.Dnn.Modules.Compatibility.Dnn5",
                                          "HeroldIT.Dnn.Modules.Compatibility.Dnn5.TabController");
                        }
                    }
                }
                return (TabController)HttpContext.Current.Items[TabControllerItemKey];
            }
        }

        #endregion

        #region abstract methods

        /// <summary>
        /// Gets the tabs for a portal.
        /// </summary>
        public abstract Dictionary<int, TabInfo> GetTabsByPortal(int portalId);

        #endregion
    }
}
